package com.imooc.newretail.bpl.ctrl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imooc.newretail.common.utils.BeanUtils;
import com.imooc.newretail.common.vo.RV;
import com.imooc.newretail.common.vo.ResultVO;
import com.imooc.newretail.bpl.dto.TenantDTO;
import com.imooc.newretail.bpl.dto.TenantUserDTO;
import com.imooc.newretail.bpl.entity.Tenant;
import com.imooc.newretail.bpl.enums.TenantStatusEnum;
import com.imooc.newretail.bpl.enums.TenantTypeEnum;
import com.imooc.newretail.bpl.service.ITenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.imooc.newretail.common.BaseController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 租户信息表 前端控制器
 * </p>
 *
 * @author Hank
 * @since 2020-06-27
 */
@RestController
@RequestMapping("/sys/tenant")
public class TenantController extends BaseController<ITenantService> {

    @Autowired
    ITenantService tenantService;

    /***
     * 注册加盟商
     * @param tenantDTO
     * @return
     */
    @PostMapping("/register/franchisee")
    public ResultVO<TenantDTO> registerFranchisee(@RequestBody TenantDTO tenantDTO) {
        Tenant tenant = BeanUtils.copyProperties(tenantDTO, Tenant.class);
        tenant.setType(TenantTypeEnum.FRANCHISEE.getCode());
        tenant.setStatus(TenantStatusEnum.ENABLE.getCode());
        return tenantService.saveOrUpdate(tenant) == true ? RV.success(true) : RV.fail("创建失败");
    }

    /***
     * 禁用租户
     * @param tenantId
     * @param remark
     * @return
     */
    @PostMapping("/disable")
    public ResultVO<TenantDTO> disableFranchisee(String tenantId, String remark) {
        return RV.success(tenantService.disableFranchisee(tenantId, remark));
    }

    /***
     * 启用租户；接收表单参数
     * @param tenantId
     * @return
     */
    @PostMapping("/enable")
    public ResultVO<TenantDTO> enableFranchisee(String tenantId) {
        return RV.success(tenantService.disableFranchisee(tenantId, ""));
    }

    /***
     * 查询租户；接收浏览器URL斜杠后的参数(获取路径参数)
     * @param tenantId
     * @return
     */
    @GetMapping("/{tenantId}")
    public ResultVO<TenantDTO> getByTenantId(@PathVariable("tenantId")String tenantId) {
        return RV.success(tenantService.getById(tenantId));
    }

    @GetMapping("/list")
    public ResultVO findList(@RequestParam("currentPage")Integer cuurentPage,@RequestParam("pageRow")Integer pageRow) {
        List<TenantDTO> tenantDTOS = new ArrayList<>();
        QueryWrapper query = new QueryWrapper();
        query.orderByAsc("create_time");
        return RV.success(tenantService.page(new Page<>(cuurentPage,pageRow),query));
    }

    @GetMapping("/{tenantId}/users")
    public ResultVO<TenantUserDTO> getUsersByTenantId(@PathVariable("tenantId") String tenantId) {
        return RV.success(tenantService.getUsersByTenantId(tenantId));
    }

    @DeleteMapping("/{tenantId}/{userId}")
    public ResultVO<TenantUserDTO> removeUser(@PathVariable("tenantId") String tenantId,@PathVariable("userId")String userId) {
        return RV.success(tenantService.getUsersByTenantId(tenantId));
    }
}